﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using StrategyTest.Strategy;

namespace StrategyTest.Common
{
    public class DayBars
    {
        private DateTime today;

        private Bar[] dayBars;

        private int index;

        private Test1 Strategy;

        public DayBars(Test1 test)
        {
            index = 0;

            today = DateTime.MinValue;

            dayBars = new Bar[240];

            Strategy = test;

            for (int i = 0; i < 240; i++)
            {
                dayBars[i] = new Bar();

                dayBars[i].OpenTime = DateTime.MinValue;
                dayBars[i].Open  = 0;
                dayBars[i].High  = 0;
                dayBars[i].Low   = 0;
                dayBars[i].Close = 0;
            }
        }

        public void Quote(Bar bar)
        {
            if (today.Date != bar.OpenTime.Date)
            {
                //运行策略
                Strategy.Run(this.dayBars);

                //清除队列
                ClearBars();

                index = 0;
                today = bar.OpenTime.Date;
            }
            else
            {
                index++;
            }

            dayBars[index].OpenTime = bar.OpenTime;
            dayBars[index].Open = bar.Open;
            dayBars[index].High = bar.High;
            dayBars[index].Low = bar.Low;
            dayBars[index].Close = bar.Close;
        }

        private void ClearBars()
        {
            for (int i = 0; i < 240; i++)
            {
                dayBars[i].OpenTime = DateTime.MinValue;
                dayBars[i].Open  = 0;
                dayBars[i].High  = 0;
                dayBars[i].Low   = 0;
                dayBars[i].Close = 0;
            }
        }
    }
}
